<template>
  <div>
    <el-row>
      <el-col>
        <my-form :formOptions="findFormOptions" :isInline="true" :isSearch="true"
          @sureSubmit="findTableData">
          <template #bottom_button>
            <el-button icon="el-icon-refresh" size="mini" type="primary" @click="reset">重置</el-button>
          </template>
        </my-form>
      </el-col>
    </el-row>
    <el-row type="flex" justify="end">
      <el-col :span="2">
        <el-button size="mini" @click="add" icon="el-icon-circle-plus" type="primary">新增</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <my-table :hasBorder="true" :hasIndex="true" :hasPagination="true" :hasStripe="true" :hasSelection="false"
          :tableColumnOptions="tableColumnOptions" :tableData="tableData">
          <template #dirDis="{data}">
            {{ fc_arr_dir(data.dirDis) }}
          </template>
          <template #operation="{ data }">
            <el-button type="text" @click="update(data)">编辑</el-button>
            <el-button type="text" @click="remove(data)">删除</el-button>
          </template>
        </my-table>
      </el-col>
    </el-row>
    <my-dialog :dialogVisible.sync="infoDialog" :title="infoTitle">
      <template #content>
        <my-form :form="isAddInfo ? copyForm : form" :formOptions="infoFormOptions"  :rules="infoRules"
          @sureSubmit="infoFormSubmit" ref="infoForm">
          <template #dirLeader="{ data }">
            <el-autocomplete v-model="test1" :fetch-suggestions="querySearch" placeholder="请输入内容"
              @select="handleSelect_person"></el-autocomplete>
          </template>
        </my-form>
      </template>
    </my-dialog>
  </div>
</template>

<script>
import {getLabelByModel,getModelByLabel} from "@/utils/metaInfo"
import request from "@/utils/request"
import myForm from "@/components/myForm.vue";
import myTable from "@/components/myTable.vue";
import myDialog from "@/components/myDialog.vue";
import {
  A_tableData,
  A_add,
  A_update,
  A_remove,
  A_getUtil,
  A_changeUtil,
  A_getDisArr
} from "@/api/infoMange/disciplineOrientation";
import { A_getUserList } from "@/utils/aboutUsers";
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "disciplineOrientation",
  inject: ["reload"],
  data() {
    return {
      findFormOptions: [{ type: "input", label: "方向名称", model: "dirName" }],
      tableColumnOptions: [
        {
          label: "学科方向名称",
          prop: "dirName",
          // width: "150",
        },
        {
          label: "学科方向编号",
          prop: "dirId",
          // width: "150",
        },
        {
          label: "所属学科名称",
          // isSlot:true,
          slotName:'dirDis',
          prop: "dirDis",
          // width: "150",
        },
        {
          label: "方向带头人工号",
          prop: "dirLeader",
          // width: "150",
        },
        {
          label: "方向带头人姓名",
          prop: "name",
          // width: "150",
        },
        {
          label: "操作",
          width: "180",
          isSlot: true,
          slotName: "operation",
          fixed: "right",
        },
      ],
      tableData: [],
      infoDialog: false,
      infoTitle: "编辑操作",
      isAddInfo: false,
      form: {},
      copyForm: {},
      nowDropItem: "",
      infoFormOptions: [
        {
          type: "input",
          label: "学科方向名称",
          model: "dirName",
        },
        {
          type: "input",
          label: "学科方向编号",
          model: "dirId",
        },
        {
          type: "select",
          label: "所属学科名称",
          model: "dirDis",
          options: [],
          width: 205
        },
        {
          type: "slot",
          slotName: 'dirLeader',
          label: "学科方向带头人",
          model: "dirLeader",
        },
      ],
      infoRules: {
        dirName: [{ required: true, message: "请输入方向名称" }],
        dirId: [{ required: true, message: "请输入方向编号" }],
        dirDis: [{ required: true, message: "请选择所属学科" }],
        dirLeader: [{ required: false, message: "请选择带头人（工号）" }],
      },
      test1: "",
      user_list: null,
      arr_discipline: [],
      arr_discipline_copy:[]
    };
  },
  components: {
    myForm,
    myTable,
    myDialog,
  },
  async created() {
    this.getTableData();
    await this.getDisArr()
    this.infoFormOptions[2].options = this.changeDisArr()
  },
  methods: {
    async getUserList() {
      let result = await A_getUserList();
      result.forEach((item) => {
        const value = item.userId + ' : ' + item.userName
        item.value = value
      });
      this.user_list = result
    },
    async findTableData(data) {
      let result = await A_tableData(data);
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async getTableData() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async reset() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    add() {
      this.isAddInfo = true;
      this.infoDialog = true;
      this.getUserList();
    },
    update(data) {
      this.isAddInfo = false;
      this.infoDialog = true;
      this.form = cloneDeep(data);
      this.getUserList();
    },
    remove(data) {
      this.$confirm("你确定要删除该信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { dirkey } = data;
          let res = await A_remove(dirkey);
          if (res.code === 1) {
            this.$message({ type: "success", message: res.data });
            this.reload();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async infoFormSubmit(data) {
     data.dirDis = getLabelByModel(data.dirDis,this.arr_discipline_copy)
      if (this.isAddInfo) {
        let res = await A_add(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      } else {
        let res = await A_update(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      }
      // this.$refs.filesUpload.submit();
    },
    async getUtil() {
      let result = await A_getUtil();
      this.nowDropItem = result;
    },
    querySearch(queryString, cb) {
      var user_list = this.user_list;
      var results = queryString
        ? user_list.filter(this.createFilter(queryString))
        : user_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (user_list) => {
        return (
          user_list.userName.indexOf(queryString) === 0
        );
      };
    },
    handleSelect_person(item) {

      this.$refs.infoForm.form.dirLeader = item.userId;
    },
    async getDisArr(){
      let result = await request({
        url:"/SciDiscipline/list",
        method:"post",
        data:{}
      })
      this.arr_discipline=result.data
    },
   changeDisArr() {
      let arr = []
      this.arr_discipline.forEach(item => {
        arr.push({ label: item.subName, model: item.subKey })
      })
      console.log("arr_discipline",arr)
      this.arr_discipline_copy = arr
      return arr
    },
    fc_arr_dir(value){
      for (var item of this.arr_discipline) {
        if (item.dirkey == value) return item.dirName;
      }
    }
  },
  computed: {},
};
</script>

<style scoped>
.upload-demo {
  display: inline-block;
  margin: 0 3px;
}
</style>